import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import twoPoints.*;


public class Main
{
	public static boolean writeObjToFile (Object o, String fileName)
	{
		try
		{
			ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream(fileName));
			oos.writeObject(o);
			oos.close();
		} catch (FileNotFoundException e)
		{
			System.out.println(e.getMessage());
			return false;
		} catch (IOException e)
		{
			System.out.println(e.getMessage());
			return false;
		}

		return true;
	}
	
	public static Object getObjFromFile (String fileName)
	{
		Object obj = null;
		ObjectInputStream ois;
		try
		{
			ois = new ObjectInputStream (new FileInputStream(fileName));
			obj = (Object) ois.readObject();
			ois.close();
		} catch (FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		} catch (IOException e)
		{
			System.out.println(e.getMessage());
		} catch (ClassNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		
		return obj;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		TwoPoint tp1 = new TwoPoint(1.1, 2.2);
		TwoPoint tp2 = new TwoPoint(3.3, 4.4);
		
		System.out.println("Objects to write:");
		System.out.println(tp1.toString());
		System.out.println(tp2.toString());
		
		Main.writeObjToFile(tp1, "tp1.dat");
		Main.writeObjToFile(tp2, "tp2.dat");
		
		System.out.println("Objects read from file:");
		
		TwoPoint tp3 = (TwoPoint) Main.getObjFromFile("tp1.dat");
		TwoPoint tp4 = (TwoPoint) Main.getObjFromFile("tp2.dat");
		System.out.println(tp3.toString());
		System.out.println(tp4.toString());
	}

}
